package com.blacktech.dbu.core.response;

import java.util.List;

/**
 * 分页响应数据封装类
 *
 * @param <T> 数据类型
 * @author Yanyunsong
 */
public class PageResponse<T> {

    /**
     * 当前页数据列表
     */
    private List<T> content;

    /**
     * 总记录数
     */
    private Long totalElements;

    /**
     * 总页数
     */
    private Integer totalPages;

    /**
     * 每页大小
     */
    private Integer size;

    /**
     * 当前页码（从0开始）
     */
    private Integer number;

    /**
     * 是否为首页
     */
    private Boolean first;

    /**
     * 是否为末页
     */
    private Boolean last;

    /**
     * 是否有下一页
     */
    private Boolean hasNext;

    /**
     * 是否有上一页
     */
    private Boolean hasPrevious;

    /**
     * 空数据构造器
     */
    public PageResponse() {
    }

    /**
     * 完整构造器
     */
    public PageResponse(List<T> content, Long totalElements, Integer totalPages,
                        Integer size, Integer number, Boolean first, Boolean last) {
        this.content = content;
        this.totalElements = totalElements;
        this.totalPages = totalPages;
        this.size = size;
        this.number = number;
        this.first = first;
        this.last = last;
        this.hasNext = !last;
        this.hasPrevious = !first;
    }

    /**
     * 从Spring Data Page对象创建
     */
    public static <T> PageResponse<T> of(org.springframework.data.domain.Page<T> page) {
        return new PageResponse<>(
                page.getContent(),
                page.getTotalElements(),
                page.getTotalPages(),
                page.getSize(),
                page.getNumber(),
                page.isFirst(),
                page.isLast()
        );
    }

    /**
     * 创建空分页响应
     */
    public static <T> PageResponse<T> empty() {
        return new PageResponse<>(List.of(), 0L, 0, 0, 0, true, true);
    }

    /**
     * 创建空分页响应（指定页大小）
     */
    public static <T> PageResponse<T> empty(Integer size, Integer number) {
        return new PageResponse<>(List.of(), 0L, 0, size, number, true, true);
    }

    /**
     * 创建自定义分页响应
     */
    public static <T> PageResponse<T> of(List<T> content, Long totalElements, Integer size, Integer number) {
        if (totalElements == null || totalElements <= 0) {
            return empty(size, number);
        }

        Integer totalPages = (int) Math.ceil((double) totalElements / size);
        Integer currentPage = number != null ? number : 0;
        Boolean first = currentPage <= 0;
        Boolean last = currentPage >= totalPages - 1;

        return new PageResponse<>(content, totalElements, totalPages, size, currentPage, first, last);
    }

    // Getters and Setters
    public List<T> getContent() {
        return content;
    }

    public void setContent(List<T> content) {
        this.content = content;
    }

    public Long getTotalElements() {
        return totalElements;
    }

    public void setTotalElements(Long totalElements) {
        this.totalElements = totalElements;
    }

    public Integer getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(Integer totalPages) {
        this.totalPages = totalPages;
    }

    public Integer getSize() {
        return size;
    }

    public void setSize(Integer size) {
        this.size = size;
    }

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    public Boolean getFirst() {
        return first;
    }

    public void setFirst(Boolean first) {
        this.first = first;
    }

    public Boolean getLast() {
        return last;
    }

    public void setLast(Boolean last) {
        this.last = last;
    }

    public Boolean getHasNext() {
        return hasNext;
    }

    public void setHasNext(Boolean hasNext) {
        this.hasNext = hasNext;
    }

    public Boolean getHasPrevious() {
        return hasPrevious;
    }

    public void setHasPrevious(Boolean hasPrevious) {
        this.hasPrevious = hasPrevious;
    }

    /**
     * 判断是否为空分页
     */
    public boolean isEmpty() {
        return content == null || content.isEmpty();
    }

    /**
     * 判断是否有数据
     */
    public boolean hasContent() {
        return !isEmpty();
    }

    @Override
    public String toString() {
        return "PageResponse{" +
                "content=" + (content != null ? content.size() + " items" : "null") +
                ", totalElements=" + totalElements +
                ", totalPages=" + totalPages +
                ", size=" + size +
                ", number=" + number +
                ", first=" + first +
                ", last=" + last +
                '}';
    }
}